/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.structuralanalysis;

import org.openaecfm.model.geometry.Axis2Placement3D;
import org.openaecfm.model.kernel.Root;
import org.openaecfm.model.structuralanalysis.RelConnectsStructuralMember;
import org.openaecfm.model.structuralanalysis.StructuralConnection;
import org.openaecfm.model.structuralanalysis.StructuralMember;
import org.openaecfm.model.structuralload.BoundaryCondition;
import org.openaecfm.model.structuralload.StructuralConnectionCondition;
import org.openaecfm.service.structuralanalysis.IRelConnectsStructuralMemberManager;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EntityInstanceNameParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.RealParameter;
import org.openaecfm.spf.model.SimpleEntityInstance;

public final class RelConnectsStructuralMemberIO extends
		AbstractEntityInstanceInputImpl<RelConnectsStructuralMember> implements
		IEntityInstanceUtilInput<RelConnectsStructuralMember>,
		IEntityInstanceOutput<RelConnectsStructuralMember>,
		IEntityInstanceUtilOutput<RelConnectsStructuralMember> {

	private IEntityInstanceUtilInput<Root> utilInput;
	private IEntityInstanceUtilOutput<Root> utilOutput;
	private IRelConnectsStructuralMemberManager manager;

	public void setManager(IRelConnectsStructuralMemberManager manager) {
		this.manager = manager;
	}

	public void setUtilOutput(IEntityInstanceUtilOutput<Root> utilOutput) {
		this.utilOutput = utilOutput;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String exportSPF(RelConnectsStructuralMember pojo,
			IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance(
				"IFCRELCONNECTSSTRUCTURALMEMBER", appDataManager.getName(pojo));

		exportSPF(pojo, ei, appDataManager);

		return ei.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected RelConnectsStructuralMember doCreateObject() {
		return new RelConnectsStructuralMember();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void exportSPF(RelConnectsStructuralMember pojo, EntityInstance ei,
			IAppDataManager appDataManager) {
		utilOutput.exportSPF(pojo, ei, appDataManager);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set RelatingStructuralMember
		sei.addParameter(new EntityInstanceNameParameter(appDataManager
				.getName(pojo.getRelatingStructuralMember())));

		// Set RelatedStructuralConnection
		sei.addParameter(new EntityInstanceNameParameter(appDataManager
				.getName(pojo.getRelatedStructuralConnection())));

		// Set AppliedCondition
		if (pojo.getAppliedCondition() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new EntityInstanceNameParameter(appDataManager
					.getName(pojo.getAppliedCondition())));
		}

		// Set AdditionalConditions
		if (pojo.getAdditionalConditions() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new EntityInstanceNameParameter(appDataManager
					.getName(pojo.getAdditionalConditions())));
		}

		// Set SupportedLength
		if (pojo.getSupportedLength() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new RealParameter(pojo.getSupportedLength()));
		}

		// Set ConditionCoordinateSystem
		if (pojo.getConditionCoordinateSystem() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new EntityInstanceNameParameter(appDataManager
					.getName(pojo.getConditionCoordinateSystem())));
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveNotReferenceParameter(EntityInstance ei,
			RelConnectsStructuralMember pojo) {
		utilInput.resolveNotReferenceParameter(ei, pojo);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set SupportedLength
		Parameter supp = sei.getParameter(8);
		if (supp.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) supp;
			pojo.setSupportedLength(r.toDouble());
		}
	}

	public void setUtilInput(IEntityInstanceUtilInput<Root> utilInput) {
		this.utilInput = utilInput;
	}

	@Override
	public void resolveReferenceParameter(EntityInstance ei,
			RelConnectsStructuralMember pojo, IAppDataManager appDataManager) {
		utilInput.resolveReferenceParameter(ei, pojo, appDataManager);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set RelatingStructuralMember
		EntityInstanceNameParameter relating = (EntityInstanceNameParameter) sei
				.getParameter(4);
		manager.setRelatingStructuralMember(pojo,
				(StructuralMember) appDataManager.getObject(relating
						.toInstanceName()));

		// Set RelatedStructuralConnection
		EntityInstanceNameParameter related = (EntityInstanceNameParameter) sei
				.getParameter(5);
		manager.setRelatedStructuralConnection(pojo,
				(StructuralConnection) appDataManager.getObject(related
						.toInstanceName()));

		// Set AppliedCondition
		Parameter app = sei.getParameter(6);
		if (app.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
			EntityInstanceNameParameter n = (EntityInstanceNameParameter) app;
			pojo.setAppliedCondition((BoundaryCondition) appDataManager
					.getObject(n.toInstanceName()));
		}

		// Set AdditionalConditions
		Parameter add = sei.getParameter(7);
		if (add.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
			EntityInstanceNameParameter n = (EntityInstanceNameParameter) add;
			pojo.setAdditionalConditions((StructuralConnectionCondition) appDataManager
					.getObject(n.toInstanceName()));
		}

		// Set ConditionCoordinateSystem
		Parameter coor = sei.getParameter(9);
		if (coor.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
			EntityInstanceNameParameter n = (EntityInstanceNameParameter) coor;
			pojo.setConditionCoordinateSystem((Axis2Placement3D) appDataManager
					.getObject(n.toInstanceName()));
		}
	}
}
